$(document).ready(function(){
  
  $("td[colType='result']").dblclick(function(){
    var mBox = getBox(this);
    if(mBox == null){
      mBox = relatedBox();
      $(this).append(mBox);
      
      var ajaxPara = {
          colAction: 'query'
      };
      ajaxPara['datatableName'] = getTableName(this);
      ajaxPara['colName'] = getColName(this);
      setTrPara(this, ajaxPara);
      var url = "./Table.do?method=run";
      $.get(url, ajaxPara, function(data){
            mBox.addData(data);
      });
    } else {
      removeBox(this);
    }
  });
  
  function setTrPara(e,para){
    var tr = findUpElement(e,"tr");
    addRowData(e,para);
    //para["AT-id"] = $(tr).attr("id").substring(5);
  }
  
  function getBox(node){
    var boxs = $(node).children('div.relatedBox');
    if(boxs.size() > 0){
      var box = boxs[0];
      $(box).children("div.relatedData").empty();
      return box;
    }
    return null;
  }
  function removeBox(node){
    var boxs = $(node).children('div.relatedBox');
    if(boxs.size() > 0){
      $(boxs).remove();
    }
  }
  
  function relatedBox(){
    var box = $("<div class='relatedBox'>");
    var dataBox = $("<div class='relatedData'>");
    var newBox = $("<div class='relatedNew'>");
    $(box).append($(dataBox));
    $(box).append($(newBox));
    $(newBox).append('<input type=text/>');

    box.addData = function(paraData){
      $(this).children("div.relatedData").append(paraData);
    }
    return box;
  }
});